package in.iqing.control.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedTransformationBuilder;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import com.squareup.picasso.Transformation;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.iqing.app.R;
import in.iqing.control.helper.ImageResourceHelper;
import in.iqing.control.helper.LogHelper;
import in.iqing.control.util.MiscUtil;
import in.iqing.model.bean.Category;

/**
 * Created by ZuoShu on 15/7/3.
 */
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.CategoryViewHolder> {
    private static final String TAG = "CategoryAdapter";
    private Context context;
    private List<Category> categories;
    private static final int COVER_RATIO = 1;
    private Transformation coverTransformation;
    private int itemWidth;
    private int itemHeight;
    private OnCategoryClickListener listener;

    public interface OnCategoryClickListener {
        public void onCategoryClick(Category category);
    }

    public CategoryAdapter(Context context) {
        this.context = context;
        calculateSize();
        coverTransformation = new RoundedTransformationBuilder()
                .cornerRadius(context.getResources().getDimension(R.dimen.round_image_radius))
                .borderWidth(context.getResources().getDimension(R.dimen.image_border_width))
                .borderColor(context.getResources().getColor(R.color.iqing_yellow))
                .oval(false)
                .build();
    }

    public void setListener(OnCategoryClickListener listener) {
        this.listener = listener;
    }

    private void calculateSize() {
        int column = context.getResources().getInteger(R.integer.category_column);
        itemWidth = (int) (
                (MiscUtil.getScreenWidth(context)
                        - 2 * context.getResources().getDimension(R.dimen.category_grid_horizontal_margin)
                        - (column - 1) * context.getResources().getDimension(R.dimen.category_grid_horizontal_spacing)
                ) / column
        );
        itemHeight = itemWidth;
        LogHelper.warn(TAG, "category item width:" + itemHeight + " height:" + itemHeight);
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    @Override
    public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category, parent, false);
        return new CategoryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CategoryViewHolder holder, int position) {
        Category category = categories.get(position);
        holder.category = category;
        RequestCreator creator;
        if (category.getId() == Category.LATEST_UPDATE) {
            holder.categoryText.setText(context.getString(R.string.category_latest_update));
            Picasso.with(context).load(R.drawable.icon_latest_update)
                    .placeholder(R.drawable.image_loading_1x1)
                    .error(R.drawable.image_error)
                    .transform(coverTransformation)
                    .resize(itemWidth, itemHeight)
                    .centerCrop()
                    .into(holder.coverImage);
        } else {
            creator = TextUtils.isEmpty(category.getCover())
                    ? Picasso.with(context).load(R.drawable.image_loading_1x1)
                    : Picasso.with(context).load(ImageResourceHelper.generateCategoryImageUrl(category.getCover()));
            creator.placeholder(R.drawable.image_loading_1x1)
                    .error(R.drawable.image_error)
                    .transform(coverTransformation)
                    .resize(itemWidth, itemHeight)
                    .centerCrop()
                    .into(holder.coverImage);
            holder.categoryText.setText(category.getName());
        }

    }

    @Override
    public int getItemCount() {
        return categories == null ? 0 : categories.size();
    }

    class CategoryViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.cover_image)
        ImageView coverImage;

        @Bind(R.id.category_text)
        TextView categoryText;

        Category category;

        public CategoryViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.category_layout)
        public void onCategoryClick(View v) {
            if (listener != null) {
                listener.onCategoryClick(category);
            }
        }
    }

}
